package com.enterprise.controller.front;

import com.enterprise.entity.Article;
import com.enterprise.entity.Service;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.ArticleService;
import com.enterprise.service.ServiceService;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 主页
 * Created by Cesiumai on 2016/6/15.
 */
@Controller
@RequestMapping("/")
public class IndexAction {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ServiceService serviceService;

    @RequestMapping({"/", "/index"})
    public String index(HttpServletRequest request) {
        indexData(request);
        return "/front/index";
    }

    private void indexData(HttpServletRequest request)
    {
        // 成功案例
        articleCommons(request, "15", "succeeList");
        // 招标信息
        serviceCommons(request, null, "zhaobiaoList");
        // 行业新闻
        articleCommons(request, "2", "newsList");
        // 政策法规
        articleCommons(request, "1", "lawList");
    }

    protected void articleCommons(HttpServletRequest request, String categoryId, String attributeName)
    {
        Article article = new Article();
        article.setCategoryId(categoryId);
        article.setOffset(0);
        article.setPageSize(8);
        PageModel page = articleService.selectPageList(article);
        request.setAttribute(attributeName, page.getList());
    }

    protected void serviceCommons(HttpServletRequest request, String categoryId, String attributeName)
    {
        Service service = new Service();
        service.setCategoryId(categoryId);
        service.setOffset(0);
        service.setPageSize(8);
        PageModel page = serviceService.selectPageList(service);
        request.setAttribute(attributeName, page.getList());
    }
}

